/*
 * @Description:
 * @Author: chenju
 * @Date: 2022-01-12 11:09:44
 * @LastEditors: qingyang
 * @LastEditTime: 2023-06-28 18:08:38
 */
import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import workbenchRoutes from './modules/workbench'
import pcfRoutes from './modules/pcf'
import eciRoutes from './modules/eci'
import systemRoutes from './modules/system'
import taaRoutes from './modules/taa'
import reportManageRoutes from './modules/reportManage'

const loadComponent = (name: string) => {
  return () => import(/* webpackChunkName: "common" */ `@/views/${name}/index.vue`)
}

export const asyncRoutes: any[] = [
  {
    path: '/login',
    name: 'login',
    component: loadComponent('login')
  },
  {
    path: '/redirect',
    name: 'Redirect',
    component: loadComponent('redirect')
  },
  // {
  //   path: '/401',
  //   name: 'NoPermission',
  //   component: loadComponent('401')
  // },
  // {
  //   path: '/404',
  //   name: 'NoFind',
  //   component: loadComponent('404')
  // },
  {
    path: '/no-permission',
    name: 'NoPermissionLogin',
    component: loadComponent('noPermission')
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'PathMatch'
  }
]

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: Layout,
    redirect: '/redirect',
    children: [
      ...workbenchRoutes,
      ...pcfRoutes,
      ...eciRoutes,
      ...systemRoutes,
      ...taaRoutes,
      ...reportManageRoutes,
      {
        path: '/404',
        name: 'NoFind',
        component: loadComponent('404'),
        meta: {
          title: '404',
          activeMenu: '',
          bgColor: '#fff',
          breadcrumb: []
        }
      },
      {
        path: '/401',
        name: 'NoPermission',
        component: loadComponent('401'),
        meta: {
          title: '401',
          activeMenu: '',
          bgColor: '#fff',
          breadcrumb: []
        }
      },
      {
        path: '/dev',
        name: 'Dev',
        component: loadComponent('dev'),
        meta: {
          title: 'Dev',
          activeMenu: '',
          bgColor: '#fff',
          breadcrumb: []
        }
      }
    ]
  },
  ...asyncRoutes
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior: (to, from, savedPosition) => {
    console.log('savedPosition', from, savedPosition)
    if (from.name && savedPosition) {
      return savedPosition
    } else {
      return { left: 0, top: 0 }
    }
  }
})

export function resetRouter(): void {
  console.log('resetRouter', router.getRoutes())
  const newRouter = router
  ;(router as any).matcher = (newRouter as any).matcher // reset router
}

export default router
